package com.icodingedu.springcloud.controller;

import com.icodingedu.springcloud.pojo.PortInfo;
import com.icodingedu.springcloud.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class FeignController implements IService {

    @Value("${server.port}")
    private String port;

    @Override
    public String sayHello() {
        return "advanced : my port is "+port;
    }

    @Override
    public PortInfo sayHello(@RequestBody PortInfo portInfo) {
        log.info("you are "+portInfo.getName()+" is "+portInfo.getPort() );
        PortInfo portInfo1 = new PortInfo();
        portInfo1.setName(portInfo.getName());
        portInfo1.setPort("port:666666");
        return portInfo1;
    }

    @Override
    public String retry(@RequestParam(name="timeout") int timeout) {
        while(timeout-->0){
            try {
                Thread.sleep(1000);
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        log.info("retry is "+port);
        return "return "+port;
    }

    @Override
    public String error() {
        log.info("***** 进入到feign-client-advanced error *****");
        throw new RuntimeException("mouse droppings");
    }

}
